//管理员数据模块
var utility=require("utility");
//数据操作模块
var sqlHelper = require('../conf/sqlHelper');

//登录验证
function validateLogin(username, password, callback) {
    var sha1Value=utility.sha1(password);

	var sqlstr = 'select * from shop_manager where username=? and pass_word=?';
	var param = [username, sha1Value];

	sqlHelper.ExecuteReader(sqlstr, param, function(result) {
		if(result) {
			if(result.length > 0) {
				callback('登录成功',result[0], true);
			} else {
				callback('登录失败',null, false);
			}
		}

	});
}

//修改密码
//修改密码先判断输入密码是否正确,才能修改,分两步操作
function modifyPass(username,password,callback){

	var sha1Value=utility.sha1(password);
	var sqlstr = 'update shop_manager set pass_word=? where username=?';
	var param = [sha1Value,username];

	sqlHelper.ExecuteUpdate(sqlstr, param, function(counts) {
		if(counts) {
			if(counts> 0) {
				callback('更新成功', true);
			} else {
				callback('更新失败', false);
			}
		}

	});
}

//验证方法
function modifyManagerPass(username,oldpass,newpass,callback){
	validateLogin(username,oldpass,function(msg,list,result){
		if(result){
			//密码验证正确
			modifyPass(username,newpass,function(message,mpresult){
				if(mpresult){
					callback(message,mpresult);
				}else{
					callback(message,mpresult);
				}
			});
		}else{
			//密码验证失败
			callback('密码验证失败',false);
		}
	});
}

exports.validateLogin = validateLogin;
exports.modifyManagerPass=modifyManagerPass;